<template>
  <div class="menu-outer">
    <div class="df jcsb aic menu-header" :class="{ 'menu-header-after': $route.query.isEdit }">
      <p class="hh-64 pdl-32 fl-center pointer" @click="$router.push('/system/menu')">
        <icon-font type="icon-arrowleft"></icon-font>
        <span class="pdl-8">返回</span>
      </p>
      <p class="fs-18">查看/预览</p>
    </div>
    <div class="menu-ctx df jcsb">
      <div class="menu-left pdl-32 overflow-xy" :class="{ bgw: showRightFlag }">
        <div v-if="showRightFlag">
          <p class="pdt-24 fs-18 font333 pdb-24">新增菜单/编辑菜单</p>
          <a-button @click="addMenu">
            <template #icon>
              <icon-font type="icon-plus"></icon-font>
            </template>
            新增菜单
          </a-button>
        </div>
      </div>
      <div class="menu-center bgw">
        <!-- 一级菜单 -->
        <div
          class="menu-item mb-20 pdt-20"
          v-for="(item, index) in list"
          :key="item.id"
          @click="menuItem(item)"
          :class="{ 'menu-center--border': item.active }"
        >
          <div class="fs-14">
            <p class="pdb-20 df aic">
              <icon-font :type="item?.icon"></icon-font>
              <span class="ml-12">{{ item.name }}</span>
            </p>
            <div v-if="item.children">
              <div v-for="inner in item.children" :key="inner.id" class="mb-16 pdl-40 font999"
                >{{ inner.name }}
              </div>
            </div>
          </div>
          <div class="menu-item-operate df fdc aic jcsb" v-if="item.active">
            <div class="circle-icon" @click="changeIndex(list, index - 1, index, index === 0)">
              <icon-font type="icon-arrowup"></icon-font>
            </div>
            <div
              class="circle-icon"
              @click="changeIndex(list, index, index + 1, index === list.length - 1)"
            >
              <icon-font type="icon-arrowdown"></icon-font>
            </div>
            <a-popconfirm title="你确定要删除吗？" @confirm="menuDel(item, index)">
              <icon-font type="icon-delete"></icon-font>
            </a-popconfirm>
          </div>
        </div>
      </div>
      <div class="menu-right bgw overflow-xy fs-12" :style="{ opacity: isParentActive ? 1 : 0 }">
        <p class="pdb-8 df aic jcsb">父级菜单 </p>
        <a-input style="width: 256px" size="large" v-model:value="currSelected.name"></a-input>
        <!-- <a-select
          size="large"
          v-model:value="queryParams.firstMenu"
          style="width: 256px"
          @change="changeSelectedItem"
        >
          <a-select-option
            v-for="item in list"
            :value="item.id"
            :key="item.id"
            placeholder="请选择一级菜单"
            >{{ item.name }}
          </a-select-option>
        </a-select> -->
        <p class="mt-10 pdb-8">菜单路径</p>
        <a-input style="width: 256px" size="large" v-model:value="currSelected.routePath"></a-input>
        <p class="mt-10 pdb-8" @click="addIcon">添加图标</p>
        <!-- 这里加入添加图标逻辑 -->
        <div class="menu-right-icon df aic jcc pointer">
          <icon-font :type="currSelected.icon ? currSelected.icon : 'icon-plus'"></icon-font>
        </div>
        <!-- 二级菜单 -->
        <div class="menu-right-icon--line">
          <div class="pdb-8 mt-10" v-for="(item, index) in currSelected?.children" :key="item.id">
            <div class="pdb-8 mt-10 df aic">
              <p>子菜单（{{ index + 1 }}）</p>
              <div class="mla df aic">
                <div
                  class="circle-icon mr-4"
                  :class="{ 'is-hide': index === 0 }"
                  @click="changeIndex(currSelected?.children, index - 1, index, index === 0)"
                >
                  <icon-font type="icon-arrowup"></icon-font>
                </div>
                <div
                  class="circle-icon mr-4"
                  @click="
                    changeIndex(
                      currSelected?.children,
                      index,
                      index + 1,
                      index === currSelected.children.length - 1
                    )
                  "
                  :class="{ 'is-hide': index === currSelected.children.length - 1 }"
                >
                  <icon-font type="icon-arrowdown"></icon-font>
                </div>
                <a-popconfirm title="你确定要删除吗？" @confirm="del(item, index)">
                  <icon-font type="icon-delete"></icon-font>
                </a-popconfirm>
              </div>
            </div>
            <a-input style="width: 256px" size="large" v-model:value="item.name"></a-input>
            <div class="pdb-8 mt-10 df aic">菜单路径</div>
            <a-input style="width: 256px" size="large" v-model:value="item.routePath"></a-input>
          </div>
          <div class="mar-auto pdt-32 df aic jcc">
            <a-button @click="addSecondMenu">添加二级菜单</a-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script lang="ts">
  import { defineComponent, reactive, toRefs, ref, onMounted } from 'vue'
  import { DynamicTable, TableQueryHeader } from '@/components/dynamic-table'
  import { useRoute } from 'vue-router'
  import { useStore } from '@/store'
  import IconFont from '@/components/iconfont/icon-font'
  import { getMenu } from '@/api/system'
  export default defineComponent({
    name: 'Markcomponent',
    components: {
      IconFont,
    },
    setup() {
      const tableRef = ref<InstanceType<typeof DynamicTable>>()

      const valueDate = ref<moment.Moment[]>([])

      const route = useRoute()

      const store = useStore()

      const state = reactive({
        list: [] as any,
        isParentActive: false, // 是否激活父级菜单
        currSelected: {} as any,
        showRightFlag: false, // 显示右侧菜单
        queryParams: {
          firstMenu: null,
        },
      })

      /**
       * @description 加载表格数据
       */

      onMounted(async () => {
        const res = await getMenu({})
        state.list = res
        // console.log()
        if (route.query.isEdit === 'true') {
          state.showRightFlag = true
        } else {
          state.showRightFlag = false
        }
      })
      // 选中不同的父级菜单
      const changeSelectedItem = (ev) => {
        state.currSelected = state.list.find((v) => v.id === ev)
        console.log(state.currSelected)
        state.list.map((v) => {
          v.id === ev ? (v.active = true) : (v.active = false)
        })
      }

      // 点击菜单
      const menuItem = (item) => {
        state.isParentActive = true
        state.currSelected = item
        state.list.map((v) => {
          v.id === item.id ? (v.active = true) : (v.active = false)
        })
      }
      // 点击添加二级菜单
      const addSecondMenu = () => {
        if (state.currSelected.children) {
          state.currSelected.children.push({
            name: '',
            routePath: '',
          })
        } else {
          state.currSelected.children = []
          state.currSelected.children.push({
            name: '',
            routePath: '',
          })
        }
      }
      // 删除菜单
      const del = (item, index) => {
        state.currSelected.children.map((v, i) => {
          if (index === i) {
            state.currSelected.children.splice(i, 1)
          }
        })
      }
      // 互换位置
      const changeIndex = (array: Array<any>, index1: number, index2: number, isTrue) => {
        console.log(array, index1, index2)
        if (index1 < 0 || index2 < 0 || isTrue) {
          return
        }
        return ([array[index1], array[index2]] = [array[index2], array[index1]])
      }
      // 删除一级菜单
      const menuDel = (item, index) => {
        state.list.map((v, i) => {
          if (index === i) {
            state.list.splice(i, 1)
          }
        })
      }
      // 新增一级菜单
      const addMenu = () => {
        state.list.map((v) => (v.active = false))
        state.list.push({
          name: '新增菜单',
          active: true,
          children: [],
        })

        state.isParentActive = true
        state.currSelected = state.list[state.list.length - 1]
      }
      // 添加图标
      const addIcon = () => {}

      return {
        ...toRefs(state),
        tableRef,
        menuItem,
        changeSelectedItem,
        addSecondMenu,
        changeIndex,
        del,
        menuDel,
        addMenu,
        addIcon,
      }
    },
  })
</script>
<style lang="scss" scoped>
  .menu-outer {
    background-color: #e5e5e5;
    width: 100%;
    height: 100vh;
    .menu-header {
      width: 100%;
      background-color: #fff;
      height: 64px;
    }
    .menu-header-after {
      &:after {
        content: '';
        width: 188px;
      }
    }
    .menu-ctx {
      height: calc(100vh - 64px);
      overflow: hidden;
      .menu-left {
        width: 272px;
      }
      .menu-center {
        position: relative;
        margin-top: 64px;
        width: 314px;
        padding: 32px 0;
        overflow-y: auto;
        .center-btn {
          position: absolute;
          top: 50%;
          right: 0;
          transform: translateY(-50%);
        }

        .menu-item {
          padding-left: 76px;
          border: 1px solid transparent;
          position: relative;
          .menu-item-operate {
            position: absolute;
            width: 32px;
            height: 120px;
            border-radius: 0 0 0 8px;
            background: #fff;
            right: 0;
            top: 0;
            border-left: 1px solid $theme-color;
            border-bottom: 1px solid $theme-color;
            padding: 24px 0;
          }
        }
        .menu-center--border {
          border: 1px solid $theme-color;
        }
      }

      .menu-right {
        width: 320px;
        padding: 32px 24px;
        .menu-right-icon {
          width: 48px;
          height: 48px;
          margin-bottom: 24px;
          border: 1px solid #d9d9d9;
          border-radius: 2px;
        }
        .menu-right-icon--line {
          border-top: 1px solid #d9d9d9;
        }
      }
    }
    .is-hide {
      color: #999;
      border: 1px solid #999;
    }
    .is-show {
      visibility: visible;
    }
    .circle-icon {
      width: 14px;
      height: 14px;
      border-radius: 50%;
      border: 1px solid #333;
      cursor: pointer;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
</style>
